package com.powernode.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.entity.SysRole;
import com.powernode.entity.SysUser;
import com.powernode.model.Result;
import com.powernode.service.SysRoleService;
import com.powernode.service.SysUserService;
import com.powernode.util.AuthUtils;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统管理员控制层
 */
@Api(tags = "系统管理员接口管理")
@RequestMapping("sys/user")
@RestController
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;
    @Resource
    private SysRoleService sysRoleService;

    @ApiOperation("查询登录的用户信息")
    @GetMapping("info")
    public Result<SysUser> loadSysUserInfo(){
        //获取用户登录标识
        Long loginUserId = AuthUtils.getLoginUserId();

        //根据用户标示查询登录用户信息
        SysUser user = sysUserService.getById(loginUserId);
        return Result.success(user);
    }



    @ApiOperation("多条件分页查询系统管理员")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:user:page')")
    public Result<Page<SysUser>> loadSysUserPage(@RequestParam Long current,
                                                 @RequestParam Long size,
                                                 @RequestParam(required = false) String username){
        //创建mP分页对象
        Page<SysUser> page = new Page<>(current,size);

        page = sysUserService.page(page, new LambdaQueryWrapper<SysUser>()
                .like(StrUtil.isNotBlank(username), SysUser::getUsername, username)
                .orderByDesc(SysUser::getCreateTime)
        );

        return Result.success(page);
    }

    @ApiOperation("新增管理员")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:user:save')")
    public Result<String> saveSysUser(@RequestBody SysUser sysUser){

        Integer count = sysUserService.saveSysUser(sysUser);

        return Result.handle(count > 0);
    }


    @ApiOperation("根据标识查询系统管理员信息")
    @GetMapping("info/{id}")
    @PreAuthorize("hasAuthority('sys:user:info')")
    public Result<SysUser> loadSysUserInfo(@PathVariable(value = "id") Long id){

       SysUser sysUser = sysUserService.querySysUserInfoByUserId(id);
       return Result.success(sysUser);

    }

    @ApiOperation("修改管理员信息")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:user:update')")
    public Result<String> modifySysUserInfo(@RequestBody SysUser sysUser){

        Integer count = sysUserService.modifySysUserInfo(sysUser);
        return Result.handle(count > 0);

    }


    @ApiOperation("批量/单个删除管理员")
    @DeleteMapping("{userIds}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public Result<String> removeSysUsers(@PathVariable List<Long> userIds){

        Boolean removed = sysUserService.removeSysUserListByUserIds(userIds);
        return Result.handle(removed);

    }

    @ApiOperation("根据标识查询角色详情")
    @DeleteMapping("info/{roleId}")
    @PreAuthorize("hasAuthority('sys:user:info')")
    public Result<SysRole> loadSysRoleInfo(@PathVariable Long roleId){

        SysRole sysRole = sysRoleService.querySysRoleInfoByRoleId(roleId);
        return Result.success(sysRole);


    }



}
